/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package semantics;

import java.util.HashMap;
import java.util.Map;
import syntax.Location;
import syntax.LocationRuntimeInfo;
import syntax.Repository;
import syntax.TIOA;
import syntax.Transition;
import syntax.TransitionRuntimeInfo;

/**
 *
 * @author ennouaary
 */
public class TIOARuntime {

    private TIOA tioa;

    public TIOARuntime(TIOA tioa) {
        this.tioa = tioa;
        Repository repo = tioa.getRepository();
        locationRuntimeInfoCollection = new HashMap<>();
        transitionRuntimeInfoCollection = new HashMap<>();
        
        for(Transition transition: repo.getAllTransitions()) {
            TransitionRuntimeInfo transInfo = new TransitionRuntimeInfo();
            transInfo.setReachable(false);
            transitionRuntimeInfoCollection.put(transition, transInfo);
        }
        
        for(Location location: repo.getAllLocations()){
            LocationRuntimeInfo locInfo = new LocationRuntimeInfo();
            locInfo.setReachable(false);
            locationRuntimeInfoCollection.put(location, locInfo);
        }        
        putLocationReachability(tioa.getInitialLocation(), true);
    }
    
    
    HashMap<Location, LocationRuntimeInfo> locationRuntimeInfoCollection;
    HashMap<Transition, TransitionRuntimeInfo> transitionRuntimeInfoCollection;

    public void addLocationRuntimeInfo(Location location, LocationRuntimeInfo locationRuntimeInfo) {
        locationRuntimeInfoCollection.put(location, locationRuntimeInfo);
    }

    public LocationRuntimeInfo getLocationRuntimeInfo(Location location) {
        return locationRuntimeInfoCollection.get(location);
    }

    public void addTransitionRuntimeInfo(Transition transition, TransitionRuntimeInfo transitionRuntimeInfo) {
        transitionRuntimeInfoCollection.put(transition, transitionRuntimeInfo);
    }

    
    public LocationRuntimeInfo putLocationReachability(Location location, boolean reachability) {
        LocationRuntimeInfo locInfo = getLocationRuntimeInfo(location);
        locInfo.setReachable(reachability);
        locationRuntimeInfoCollection.put(location, locInfo);
        return locationRuntimeInfoCollection.get(location);
    }
    
    
    public TransitionRuntimeInfo putTransitionReachability(Transition transition, boolean reachability) {
        TransitionRuntimeInfo transInfo = getTransitionRuntimeInfo(transition);
        transInfo.setReachable(reachability);
        transitionRuntimeInfoCollection.put(transition, transInfo);
        return transitionRuntimeInfoCollection.get(transition);
    }

    public TransitionRuntimeInfo putTransitionNewAddedState(Transition transition, State state) {
        TransitionRuntimeInfo transInfo = getTransitionRuntimeInfo(transition);
        transInfo.getLastStatesPool().add(state);
        transitionRuntimeInfoCollection.put(transition, transInfo);
        return transitionRuntimeInfoCollection.get(transition);
    }
    
    public TransitionRuntimeInfo getTransitionRuntimeInfo(Transition transition) {
        return transitionRuntimeInfoCollection.get(transition);
    }

    public TIOA getTioa() {
        return tioa;
    }

    public void setTioa(TIOA tioa) {
        this.tioa = tioa;
    }
    
    public String printReachability(){
        StringBuilder stb = new StringBuilder();
        stb.append("REACHABILITY ANALYSIS RESULTS\n");
        stb.append("LOCATIONS\n");
        for(Map.Entry<Location, LocationRuntimeInfo> entry : locationRuntimeInfoCollection.entrySet()){
            LocationRuntimeInfo locInfo = entry.getValue();
            Location location = entry.getKey();
            stb.append(location);
            stb.append(":");
            stb.append(locInfo.isReachable());
            stb.append("\n");
        }
        stb.append("TRANSITIONS\n");
        for(Map.Entry<Transition, TransitionRuntimeInfo> entry : transitionRuntimeInfoCollection.entrySet()){
            TransitionRuntimeInfo transInfo = entry.getValue();
            Transition transition = entry.getKey();
            stb.append(transition);
            stb.append(":");
            stb.append(transInfo.isReachable());
            stb.append("\n");
        }
        
        return stb.toString();    
    }
    
}
